package kids.movies.hoathinhHDSRC.view;

import java.util.ArrayList;
import java.util.List;

import kids.movies.hoathinhHD.R;
import kids.movies.hoathinhHDSRC.E4KidsConfig;
import kids.movies.hoathinhHDSRC.E4KidsSlidingMenuActivity;
import kids.movies.hoathinhHDSRC.adapter.DetailVideoPagerAdapter;
import kids.movies.hoathinhHDSRC.fragment.IndexNewFreagment;
import kids.movies.hoathinhHDSRC.fragment.IndexViewFragment;
import kids.movies.hoathinhHDSRC.fragment.PlaylistFragment;
import kids.movies.hoathinhHDSRC.util.TabFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

public class CatDetailActivity extends E4KidsSlidingMenuActivity implements
		OnTabChangeListener, OnPageChangeListener, OnClickListener {
	private TabHost mTabHost;
	private ViewPager pager;

	private ImageView ivABCancelSearch;
	private ImageView ivABSearch;
	private EditText edtABSearch;
	private LinearLayout llContainEdt;
	private ImageView tvABTitle;
	private ImageView ivABLogo;
	private AdView adView;
	private InterstitialAd interstitial;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_index);

		// NHGlobal.config(getApplicationContext());
		// setContentView(R.layout.layout_detail_video_pager);

		this.setBehindContentView(R.layout.frame_layout_container);
		// this.getSlidingMenu().setSecondaryMenu(R.layout.frame_right_container);
		this.setDefaultBar();
		configLeftSlideMenu();
		// this.removeSlidingMenu();

		init();

		List<Fragment> listFragments = new ArrayList<Fragment>();
		listFragments.add(new PlaylistFragment());
		listFragments.add(new IndexNewFreagment());
		listFragments.add(new IndexViewFragment());

		DetailVideoPagerAdapter adapter = new DetailVideoPagerAdapter(
				CatDetailActivity.this, getSupportFragmentManager(),
				listFragments);

		pager.setAdapter(adapter);
		pager.setOffscreenPageLimit(2);
		pager.setOnPageChangeListener(this);

	}

	private void init() {
		interstitial = new InterstitialAd(getBaseContext());
		interstitial.setAdUnitId(getString(R.string.admod));
		adView = (AdView) this.findViewById(R.id.ad);
		AdRequest adRequest = new AdRequest.Builder()
				.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("")
				.build();
		adView.loadAd(adRequest);
		interstitial.loadAd(adRequest);
		interstitial.setAdListener(new AdListener() {
			@Override
			public void onAdLoaded() {
			}
		});
		ivABCancelSearch = actionBar.getIvABCancelSearch();
		ivABSearch = actionBar.getIvABSearch();
		edtABSearch = actionBar.getEdtABSearch();
		llContainEdt = actionBar.getLlContainEdt();
		ivABLogo = actionBar.getIvABLogo();
		tvABTitle = actionBar.getIvTitle();

		ivABLogo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				toggle();
			}
		});

		// edtABSearch.setOnEditorActionListener(searchAction);
		ivABSearch.setOnClickListener(this);
		ivABCancelSearch.setOnClickListener(this);

		edtABSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
		edtABSearch.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView arg0, int actionID,
					KeyEvent arg2) {
				String text = edtABSearch.getText().toString().trim();
				if (!text.equals("") || !TextUtils.isEmpty(text)) {

					goActivitySearch(CatDetailActivity.this,
							SearchResultActivity.class,
							E4KidsConfig.KEY_INTENT_SEARCH, text);
				}
				return true;
			}
		});

		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setOnTabChangedListener(this);
		pager = (ViewPager) findViewById(R.id.cate_info_pager);

		mTabHost.setup();
		addTab();
	}

	private void addTab() {
		View tabIndicator = LayoutInflater.from(CatDetailActivity.this)
				.inflate(R.layout.tab_tabhost, mTabHost.getTabWidget(), false);
		View tabIndicator1 = LayoutInflater.from(CatDetailActivity.this)
				.inflate(R.layout.tab_tabhost, mTabHost.getTabWidget(), false);
		View tabIndicator2 = LayoutInflater.from(CatDetailActivity.this)
				.inflate(R.layout.tab_tabhost, mTabHost.getTabWidget(), false);
		TextView tvNew = (TextView) tabIndicator.findViewById(R.id.title);
		TextView tvIndex = (TextView) tabIndicator1.findViewById(R.id.title);
		TextView tvlike = (TextView) tabIndicator2.findViewById(R.id.title);

		tvNew.setText(getString(R.string.cat_detail_v_new));
		tvIndex.setText(getString(R.string.cat_detail_playlist));
		tvlike.setText(getString(R.string.cat_detail_most_view));

		TabHost.TabSpec tSpecIndex = mTabHost
				.newTabSpec(getString(R.string.cat_detail_playlist));
		tSpecIndex.setIndicator(tabIndicator1);
		tSpecIndex.setContent(new TabFactory(CatDetailActivity.this));
		mTabHost.addTab(tSpecIndex);

		TabHost.TabSpec tSpecNew = mTabHost
				.newTabSpec(getString(R.string.cat_detail_v_new));
		tSpecNew.setIndicator(tabIndicator);

		tSpecNew.setContent(new TabFactory(CatDetailActivity.this));
		mTabHost.addTab(tSpecNew);

		TabHost.TabSpec tSpecLike = mTabHost
				.newTabSpec(getString(R.string.cat_detail_most_view));
		tSpecLike.setIndicator(tabIndicator2);
		tSpecLike.setContent(new TabFactory(CatDetailActivity.this));
		mTabHost.addTab(tSpecLike);

	}

	@Override
	public void onTabChanged(String arg0) {
		int pos = this.mTabHost.getCurrentTab();
		this.pager.setCurrentItem(pos);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int arg0) {
		if (arg0 == 0) {
			enableSlidingMenu();
		} else {
			this.removeSlidingMenu();
		}
		this.mTabHost.setCurrentTab(arg0);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.iv_search:
			if (llContainEdt.getVisibility() == View.GONE) {
				llContainEdt.setVisibility(View.VISIBLE);
				ivABSearch.setVisibility(View.GONE);
				tvABTitle.setVisibility(View.GONE);
				edtABSearch.setFocusable(true);
				edtABSearch.requestFocus();
				showBoard(edtABSearch);
			}
			break;
		case R.id.iv_cancel:
			String text = edtABSearch.getText().toString().trim();
			if (!text.equals("")) {
				edtABSearch.setText("");
			} else {
				llContainEdt.setVisibility(View.GONE);
				ivABSearch.setVisibility(View.VISIBLE);
				tvABTitle.setVisibility(View.VISIBLE);
				edtABSearch.setFocusable(true);
				hiddenBoard(edtABSearch);
			}
			break;

		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		app.setCatID(-1);
	}
}
